package entity;

import gui.View;
import map.*;

public abstract class Entity {

	protected View view;
	
	private Tile currentTile;
	
	private Layer mapLayer;
	
	private int hp;
	
	private int[] position = new int[2];
	
	private char dir;
	
	private Move move = null;
	
	
	public Tile getTile() {
		return currentTile;
	}
	
	public void setTile(int x, int y) {
		currentTile = mapLayer.getTile(x, y);
		position[0] = currentTile.getLocalX()*View.TILE_SIZE;
		position[1] = currentTile.getLocalY()*View.TILE_SIZE;
	}
	
	public void setTile(Tile tile) {
		currentTile = tile;
		position[0] = currentTile.getLocalX()*View.TILE_SIZE;
		position[1] = currentTile.getLocalY()*View.TILE_SIZE;
	}

	public void setPosition() {
		position[0] = currentTile.getLocalX()*View.TILE_SIZE;
		position[1] = currentTile.getLocalY()*View.TILE_SIZE;
	}
	
	public void setLayer(Layer l) {
		mapLayer = l;
	}
	
	public void moveNorth() {
		if (getTile().getLocalY() != 1) {
			Tile nextTile = mapLayer.getTileNorth(currentTile);
			if (!nextTile.getProperties().getBlocked()) {
				if (move == null || !move.isAlive()) {	
					move = new Move(this, position[1],
							nextTile.getLocalY()*View.TILE_SIZE);
					currentTile = nextTile;
					dir = 'n';
					move.start();
				}
			}
		} else {
			Region region = mapLayer.getRegion().getMap().getRegionNorth(mapLayer.getRegion());
			view.loadRegion(region,'u');
		}
	}
	
	public void moveSouth() {
		if (getTile().getLocalY() != View.TILES - 1) {
			Tile nextTile = mapLayer.getTileSouth(currentTile);
			if (!nextTile.getProperties().getBlocked()) {
				if (move == null || !move.isAlive()) {	
					move = new Move(this, position[1],
							nextTile.getLocalY()*View.TILE_SIZE);
					currentTile = nextTile;
					dir = 's';
					move.start();
				}
			}
		} else {
			Region region = mapLayer.getRegion().getMap().getRegionSouth(mapLayer.getRegion());
			view.loadRegion(region,'d');
		}
	}
	
	public void moveEast() {
		if (getTile().getLocalX() != View.TILES-1) {
			Tile nextTile = mapLayer.getTileEast(currentTile);
			if (!nextTile.getProperties().getBlocked()) {
				if (move == null || !move.isAlive()) {
					move = new Move(this, position[0],
							nextTile.getLocalX()*View.TILE_SIZE);
					currentTile = nextTile;
					dir = 'e';
					move.start();
				}
			}
		} else {
			Region region = mapLayer.getRegion().getMap().getRegionEast(mapLayer.getRegion());
			view.loadRegion(region,'r');
		}
	}
	
	public void moveWest() {
		if (getTile().getLocalX() != 0) {
			Tile nextTile = mapLayer.getTileWest(currentTile);
			if (!nextTile.getProperties().getBlocked()) {
				if (move == null || !move.isAlive()) {		
					move = new Move(this, position[0],
							nextTile.getLocalX()*View.TILE_SIZE);
					currentTile = nextTile;
					dir = 'w';
					move.start();
				}
			}
		} else {
			Region region = mapLayer.getRegion().getMap().getRegionWest(mapLayer.getRegion());
			view.loadRegion(region,'l');
		}
	}
	
	public int[] getPosition() {
		return position;
	}
	
	public void updatePosition(int pos) {
		switch(dir) {
		case 'n':
			if (view.getMapPosition()[1]+pos < view.getHeight()/2 &&
					view.getMapPosition()[1] < -View.TILE_SIZE) {
				view.updateMapPosition(0, 1);
			}
				position[1] = pos;
			break;
		case 's':
			if (view.getMapPosition()[1]+pos > view.getHeight()/2 &&
					view.getMapPosition()[1]+(View.TILES*View.TILE_SIZE) >
						view.getHeight()+View.TILE_SIZE) {
				view.updateMapPosition(0, -1);
			}
				position[1] = pos;
			break;
		case 'e':
			if (view.getMapPosition()[1]+pos > view.getWidth()/2 &&
					view.getMapPosition()[0]+(View.TILES*View.TILE_SIZE) >
						view.getWidth()+View.TILE_SIZE) {
				view.updateMapPosition(-1, 0);
			}
				position[0] = pos;
			break;
		case 'w':
			if (view.getMapPosition()[0]+pos < view.getWidth()/2 &&
					view.getMapPosition()[0] < -View.TILE_SIZE) {
				view.updateMapPosition(1, 0);
			}
				position[0] = pos;
			break;
		}
		view.repaint();
	}
	
}
